﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;


namespace ZBJF.Warranty.TheThirdPartyInteraction.Sellers.ChiDu
{
    public class ChiDuAesHelper
    {
        string _aesIv = "!!13030882113^^^";
        string rtxt;
        RijndaelManaged _rm;
        byte[] _intxtsArr;
        byte[] keyArray;
        byte[] ivArray;
        byte[] _resultArr;
        /// <summary>
        /// 实例化aes加密和解密类
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="iv">iv向量</param>
        public ChiDuAesHelper(string key, string iv = "")
        {
            keyArray = Encoding.UTF8.GetBytes(key.Length == 32 ? key : Md5Sha1(key));
            ivArray = Encoding.UTF8.GetBytes(iv.Length == 16 ? iv : _aesIv);
            _rm = new RijndaelManaged();
            _rm.Key = keyArray;
            _rm.IV = ivArray;
            _rm.Mode = CipherMode.CBC;
            _rm.Padding = PaddingMode.PKCS7;
        }

        /// <summary>
        /// 加解密操作
        /// </summary>
        /// <param name="lb">1加密，2解密</param>
        private void aesCrypt(int lb)
        {
            ICryptoTransform cTransform;
            switch (lb)
            {
                case 1:
                    cTransform = _rm.CreateEncryptor();
                    break;
                case 2:
                    cTransform = _rm.CreateDecryptor();
                    break;
                default:
                    cTransform = _rm.CreateEncryptor();
                    break;

            }
            _resultArr = cTransform.TransformFinalBlock(_intxtsArr, 0, _intxtsArr.Length);

        }
        /// <summary>
        /// aes加密
        /// </summary>
        /// <param name="inTxts">未加密的原文</param>
        /// <returns>返回加密后密文</returns>
        public string Encrypt(string inTxts)
        {
            _intxtsArr = Encoding.UTF8.GetBytes(inTxts);
            aesCrypt(1);
            return Convert.ToBase64String(_resultArr, 0, _resultArr.Length);
        }
        /// <summary>
        /// aes解密
        /// </summary>
        /// <param name="inTxts">已加密的密文</param>
        /// <returns>返还解密后原文</returns>
        public string Decrypt(string inTxts)
        {
            try
            {
                _intxtsArr = Convert.FromBase64String(inTxts);
                aesCrypt(2);
                rtxt = Encoding.UTF8.GetString(_resultArr);
            }
            catch { rtxt = "解密失败"; }
            return rtxt;

        }

        /// <summary>
        /// 哈希加密处理
        /// </summary>
        /// <param name="t_intxt">待加密的原始文字</param>
        /// <param name="t_cryType">加密类型,支持三种: md5,sha1,sha256</param>
        /// <param name="t_addTxt">加盐的文字</param>
        /// <param name="isDaxie">是否返回大写,1是0否,默认1</param>
        /// <returns></returns>
        public static string Md5Sha1(string t_intxt, string t_cryType = "md5", string t_addTxt = "", int isDaxie = 0)
        {

            if (t_intxt.Length > 0)
            {
                byte[] bytArray = new byte[0];
                if (t_addTxt.Length > 0) { t_intxt = string.Format("{0}{1}", t_addTxt, t_intxt); }
                byte[] oTxtArray = System.Text.Encoding.UTF8.GetBytes(t_intxt);
                switch (t_cryType)
                {
                    case "md5":
                        MD5 md5 = new MD5CryptoServiceProvider();
                        bytArray = md5.ComputeHash(oTxtArray);
                        break;
                    case "sha1":
                        SHA1 sha1 = new SHA1CryptoServiceProvider();
                        bytArray = sha1.ComputeHash(oTxtArray);
                        break;
                    case "sha256":
                        SHA256 sha256 = new SHA256CryptoServiceProvider();
                        bytArray = sha256.ComputeHash(oTxtArray);
                        break;
                }
                string strResult = BitConverter.ToString(bytArray).Replace("-", "");

                return isDaxie == 1 ? strResult.ToUpper() : strResult.ToLower();
            }
            else { return ""; }

        }
    }
}
